<template>
  <div>
    <el-row>
      <el-col
        :span="8"
        v-for="item in props.list"
        :key="item.idName + item.teamPlayerId"
        class="info-item"
      >
        <img :src="item.idPhoto" style="width: 90px;height: 90px;" />
        <div class="info-right">
          <div class="base-info">
            <div>{{ item.idName }}</div>
            <div v-if="item.identityType === TeamMemberRoleEnum.PLAYER">
              {{ item.playerNumber }}号
            </div>
          </div>
          <div>
            <el-button @click="showDialog(1, item)" class="info-btn"
              >查看资料</el-button
            >
            <el-button
              v-if="props.canEdit"
              type="primary"
              plain
              @click="showDialog(2, item)"
              class="info-btn"
              >编辑资料</el-button
            >
          </div>
        </div>
      </el-col>
    </el-row>

    <el-dialog v-model="state.dialogVisible" :title="dialogTitle" width="55%">
      <div>
        <el-descriptions class="margin-top" :column="4" border>
          <el-descriptions-item label="证件照">
            <img :src="state.currentInfo.idPhoto" style="width: 90px;height: 90px;" />
          </el-descriptions-item>
          <el-descriptions-item label="姓名">
            {{ state.currentInfo.idName }}
          </el-descriptions-item>
          <el-descriptions-item label="性别">
            {{ state.currentInfo.gender ?  state.currentInfo.gender === "man" ? "男" : "女" : '-' }}
          </el-descriptions-item>
          <el-descriptions-item label="国籍">
            {{ nationalityText }}
          </el-descriptions-item>
          <el-descriptions-item label="证件号">
            <span v-show="state.dialogType === 1">
              <span v-if="state.currentInfo.idNumber">{{
                state.currentInfo.idNumber
              }}</span>
              <span v-else>-</span>
            </span>
            <el-input
              v-show="state.dialogType === 2"
              v-model="state.currentInfo.idNumber"
              placeholder="请输入证件号"
            />
          </el-descriptions-item>
          <el-descriptions-item label="联系方式">
            <span v-show="state.dialogType === 1">
              <span v-if="state.currentInfo.phoneNumber">{{
                state.currentInfo.phoneNumber
              }}</span>
              <span v-else>-</span>
            </span>
            <el-input
              v-show="state.dialogType === 2"
              v-model="state.currentInfo.phoneNumber"
              placeholder="请输入联系方式"
            />
          </el-descriptions-item>
          <el-descriptions-item
            label="球员号码"
            v-if="state.currentInfo.identityType === TeamMemberRoleEnum.PLAYER"
          >
            <span v-show="state.dialogType === 1">
              <span v-if="state.currentInfo.playerNumber">{{
                state.currentInfo.playerNumber
              }}</span>
              <span v-else>-</span>
            </span>
            <el-input
              v-show="state.dialogType === 2"
              v-model="state.currentInfo.playerNumber"
              placeholder="请输入球员号码"
            />
          </el-descriptions-item>
          <el-descriptions-item label="身高">
            <span v-if="state.currentInfo.height"
              >{{ state.currentInfo.height }}cm</span
            >
            <span v-else>-</span>
          </el-descriptions-item>
          <el-descriptions-item label="体重">
            <span v-if="state.currentInfo.weight"
              >{{ state.currentInfo.weight }}kg</span
            >
            <span v-else>-</span>
          </el-descriptions-item>
          <el-descriptions-item label="所属单位/学校">
            <el-tag size="small" v-if="state.currentInfo.currentEmployer">{{
              state.currentInfo.currentEmployer
            }}</el-tag>
            <span v-else>-</span>
          </el-descriptions-item>
          <el-descriptions-item label="职位">
            <span v-if="state.currentInfo.position">{{
              state.currentInfo.position
            }}</span>
            <span v-else>-</span>
          </el-descriptions-item>
        </el-descriptions>
        <div
          class="descriptions-item"
          v-if="
            state.currentInfo.idCardFrontPhoto ||
            state.currentInfo.idCardReversePhoto
          "
        >
          <div class="descriptions-label">身份证正反面</div>
          <img class="id-img" :src="state.currentInfo.idCardFrontPhoto" />
          <img class="id-img" :src="state.currentInfo.idCardReversePhoto" />
        </div>
        <div class="descriptions-item" v-if="props.entryInsurance">
          <div class="descriptions-label">参赛保险</div>
          <img class="id-img" :src="props.entryInsurance" />
        </div>
        <div
          class="descriptions-item"
          v-if="state.currentInfo.healthCertificate"
        >
          <div class="descriptions-label">体检证明</div>
          <img class="id-img" :src="state.currentInfo.healthCertificate" />
        </div>
      </div>
      <template #footer v-if="state.dialogType === 2">
        <span class="dialog-footer">
          <el-button @click="state.dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleUpdate">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { computed, reactive } from "vue";
import { teamAuditUpdate } from "@/api/match";
import { ElMessage } from "element-plus";
import { TeamMemberRoleEnum } from "../../../config/enum";
import { nationalityList } from "@/config/data";

const props = defineProps<{
  list: any[];
  canEdit: boolean
  entryInsurance?: string | null;
}>();
const state = reactive<{
  dialogVisible: boolean;
  dialogType: number;
  currentInfo: any | null;
}>({
  dialogVisible: false,
  dialogType: 1, // 1查看资料；2编辑资料
  currentInfo: null,
});

const dialogTitle = computed(() => {
  return state.dialogType === 1 ? "查看资料" : "编辑资料";
});

const nationalityText = computed(() => {
  const _nationality = nationalityList.find(item => item.value === state.currentInfo.nationality)  
  return _nationality ? _nationality.text : state.currentInfo.nationality
})

const showDialog = (type: number, item: any) => {
  state.dialogVisible = true;
  state.dialogType = type;
  state.currentInfo = item;
};

/**
 * 更新球队成员信息
 */
const handleUpdate = () => {
  teamAuditUpdate(state.currentInfo).then((res) => {
    if (res.code === 200) {
      ElMessage.success(res.msg);
      state.dialogVisible = false;
    } else {
      ElMessage.warning(res.msg);
    }
  });
};
</script>

<style lang="scss" scoped>
.info-item {
  display: flex;
  margin-bottom: 15px;

  img {
    width: 100px;
  }

  .info-right {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    margin-left: 10px;
    margin-right: 20px;
  }

  .base-info {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
  }

  .info-btn {
    display: block;
  }

  .el-button + .el-button {
    margin-left: 0px;
    margin-top: 6px;
  }

  .el-button {
    padding: 3px 8px;
  }
}

.descriptions-label {
  font-weight: 700;
  color: #606266;
  font-size: 14px;
  margin: 5px 0;
}

.id-img {
  width: 200px;
  height: 130px;
}

.id-img + .id-img {
  margin-left: 10px;
}

.descriptions-item {
  border: 1px solid #ebeef5;
  border-top: transparent;
  padding: 8px;
}
</style>